﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Browse_Item : System.Web.UI.Page
{
    private void FetchProvName(Int32 ProvId)
    {
        using (PublicCatalogDataContext de = new PublicCatalogDataContext())
        {
            var myProvenance = from Provenance in de.Provenances
                               where Provenance.ProvenanceId == ProvId
                               select new
                               {
                                   Provenance.ProvenanceName
                               };
            foreach (var b in myProvenance)
            {
                if (b.ProvenanceName != null)
                {
                    String provName = b.ProvenanceName;
                    ltProvName.Text = b.ProvenanceName;
                    ltProvName.Visible = true;
                    lblProvName.Visible = true;
                }
            }
        }
    }

    private void FetchProvId(Int32 sId)
    {
        using (PublicCatalogDataContext dc = new PublicCatalogDataContext())
        {
            var mySeries = from Series in dc.Series
                           where Series.SeriesId == sId
                           select new
                           {
                               Series.ProvenanceId,
                               Series.SeriesTitle
                           };
            foreach (var b in mySeries)
            {
                if (b.SeriesTitle != null)
                {
                    ltSeriesTitle.Text = b.SeriesTitle;
                    ltSeriesTitle.Visible = true;
                    lblSeriesTitle.Visible = true;
                }
                if (b.ProvenanceId != null) //Probably not correct matching value
                {
                    Int32 provId = b.ProvenanceId;
                    FetchProvName(provId); //Calls the LINQ query to get and set the Provenance Name.
                }
            }
        }
    }

    private void FetchFilingUnitName(int fId)
    {
        using (PublicCatalogDataContext df = new PublicCatalogDataContext())
        {
            var myFilingUnit = from FilingUnit in df.FilingUnits
                               where FilingUnit.FilingUnitId == fId
                               select new
                               {
                                   FilingUnit.FilingUnitName
                               };
            foreach (var c in myFilingUnit)
            {
                if (c.FilingUnitName != null)
                {
                    String filingName = c.FilingUnitName;
                    ltFilingUnit.Text = c.FilingUnitName;
                    ltFilingUnit.Visible = true;
                    lblFilingUnit.Visible = true;
                }
            }
        }
    }

    private void FetchItemKeyword(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dh = new PublicCatalogDataContext())
        {
            var myItemKeyword = from a in dh.ItemKeywords
                                where a.ItemId == SelectedThreadID
                                select new
                                {
                                    a.Keyword
                                };
            foreach (var f in myItemKeyword)
            {
                if (f.Keyword != null)
                {
                    rptKeyword.Visible = true;
                    lblKeyword.Visible = true;
                    rptKeyword.DataSource = myItemKeyword;
                    rptKeyword.DataBind();
                }
            }
        }
    }
    private void FetchItemParty(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dg = new PublicCatalogDataContext())
        {
            var myItemParty = from a in dg.ItemParties
                              where a.ItemId == SelectedThreadID
                              join b in dg.Qualifiers on a.QualifierId equals b.QualifierId
                              select new
                              {
                                  a.ItemPartyValue,
                                  b.QualifierType
                              };
            foreach (var d in myItemParty)
            {
                if (d.ItemPartyValue != null)
                {
                    rptItemParty.Visible = true;
                    lblItemParty.Visible = true;
                    rptItemParty.DataSource = myItemParty;
                    rptItemParty.DataBind();
                }
            }
        }
    }

    private void FetchItemGeoLocation(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext di = new PublicCatalogDataContext())
        {
            var myItemGeoLoc = from a in di.ItemGeoLocation_LINKs
                               where a.ItemId == SelectedThreadID
                               join b in di.GeoLocations on a.GeoLocationId equals b.GeoLocationId
                               select new
                               {
                                   b.GeoLocationValue
                               };
            foreach (var d in myItemGeoLoc)
            {
                if (d.GeoLocationValue != null)
                {
                    rptItemGeoLoc.Visible = true;
                    lblItemGeoLoc.Visible = true;
                    rptItemGeoLoc.DataSource = myItemGeoLoc;
                    rptItemGeoLoc.DataBind();
                }
            }
        }
    }

    private void FetchRelatedItem(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dk = new PublicCatalogDataContext())
        {
            var myRelatedItem = from a in dk.RelatedItems
                                where a.ItemId == SelectedThreadID
                                join b in dk.RelatedItemTypes on a.RelatedItemTypeId equals b.RelatedItemTypeId
                                join d in dk.Items on a.RelatedItemId equals d.ItemId
                                select new
                                {
                                    b.RelatedItemTypeName,
                                    d.ItemTitle,
                                    d.ItemId
                                };
            foreach (var f in myRelatedItem)
            {
                if (f.RelatedItemTypeName != null)
                {
                    rptRelatedItems.Visible = true;
                    lblRelatedItems.Visible = true;
                    rptRelatedItems.DataSource = myRelatedItem;
                    rptRelatedItems.DataBind();
                }
            }
        }
    }

    private void FetchItemSubject(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dh = new PublicCatalogDataContext())
        {
            var myItemSubject = from a in dh.ItemSubject_LINKs
                                where a.ItemId == SelectedThreadID
                                join b in dh.Subjects on a.SubjectId equals b.SubjectId
                                join c in dh.Qualifiers on a.QualifierId equals c.QualifierId
                                select new
                                {
                                    b.SubjectName,
                                    c.QualifierType
                                };
            foreach (var d in myItemSubject)
            {
                if (d.SubjectName != null)
                {
                    rptItemSubject.Visible = true;
                    lblItemSubject.Visible = true;
                    rptItemSubject.DataSource = myItemSubject;
                    rptItemSubject.DataBind();
                }
            }
        }
    }

    private void FetchItemVariantTitle(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dm = new PublicCatalogDataContext())
        {
            var myItemVarTitle = from a in dm.ItemVariantTitles
                                 where a.ItemId == SelectedThreadID
                                 select new
                                 {
                                     a.ItemVariantTitle1
                                 };
            foreach (var f in myItemVarTitle)
            {
                if (f.ItemVariantTitle1 != null)
                {
                    rptItemVarTitle.Visible = true;
                    lblItemVarTitle.Visible = true;
                    rptItemVarTitle.DataSource = myItemVarTitle;
                    rptItemVarTitle.DataBind();
                }
            }
        }
    }

    private void FetchItemDate(Int32 SelectedThreadID)
    {
        using (PublicCatalogDataContext dl = new PublicCatalogDataContext())
        {
            var myItemDate = from a in dl.ItemDates
                             where a.ItemId == SelectedThreadID
                             join b in dl.Qualifiers on a.QualifierId equals b.QualifierId
                             select new
                             {
                                 a.ItemDateValue,
                                 b.QualifierType
                             };
            foreach (var d in myItemDate)
            {
                if (d.ItemDateValue != null)
                {
                    rptItemDate.Visible = true;
                    lblItemDate.Visible = true;
                    rptItemDate.DataSource = myItemDate;
                    rptItemDate.DataBind();
                }
            }
        }
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
            Int32 SelectedThreadID = Int32.Parse(Request.QueryString["query"]);
            using (PublicCatalogDataContext dz = new PublicCatalogDataContext())
            {
                var myItemDip = from Item in dz.Items
                             where Item.ItemId == SelectedThreadID
                             select new
                             {
                                Item.ItemReceivedFileName,
                                Item.SuperPackageId
                             };
                foreach (var z in myItemDip)
                {
                    if (z.ItemReceivedFileName != null)
                    {
                          Response.Redirect ("http://aslaprpedmanifest.ad.local/dip/"+z.SuperPackageId+"/"+z.ItemReceivedFileName); 

                    }
                }
            }
 
   }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["query"] != null)
        {
            Int32 SelectedThreadID = Int32.Parse(Request.QueryString["query"]);
            using (PublicCatalogDataContext db = new PublicCatalogDataContext())
            {
                var myItem = from Item in db.Items
                             where Item.ItemId == SelectedThreadID
                             select new
                             {
                                 Item.ItemTitle,
                                 Item.ItemDisplayTitle,
                                 Item.SeriesId,
                                 Item.FilingUnit,
                                 Item.ItemExtentDescription,
                                 Item.ItemDescription,
                                 Item.ItemSample,
                                 Item.ItemGeneralNote,
                                 Item.ItemOriginatorIdentifier,
                                 Item.FilingUnitId,
                                 Item.ItemPURL
                             };

                foreach (var a in myItem)
                {
                    Int32 sId = a.SeriesId;
                    FetchProvId(sId); //Uses the sId to get the Series Title and Provenance Name
                    FetchItemParty(SelectedThreadID);
                    FetchItemKeyword(SelectedThreadID);
                    FetchItemSubject(SelectedThreadID);
                    FetchItemGeoLocation(SelectedThreadID);
                    FetchRelatedItem(SelectedThreadID);
                    FetchItemDate(SelectedThreadID);
                    FetchItemVariantTitle(SelectedThreadID);

                    if (a.FilingUnitId != null)
                    {
                        int fId = (int)a.FilingUnitId;
                        FetchFilingUnitName(fId);
                    }
                    if (a.ItemTitle != null)
                    {
                        ltItemTitle.Text = a.ItemTitle;
                        ltItemTitle.Visible = true;
                        lblItemTitle.Visible = true;
                    }
                    if (a.ItemDisplayTitle != null)
                    {
                        ltItemDisplayTitle.Text = a.ItemDisplayTitle;
                        ltItemDisplayTitle.Visible = true;
                        lblItemDisplayTitle.Visible = true;
                    }
                    if (a.ItemExtentDescription != null)
                    {
                        ltItemExtDesc.Text = a.ItemExtentDescription;
                        ltItemExtDesc.Visible = true;
                        lblItemExtDesc.Visible = true;
                    }
                    if (a.ItemDescription != null)
                    {
                        ltItemDesc.Text = a.ItemDescription;
                        ltItemDesc.Visible = true;
                        lblItemDesc.Visible = true;
                    }
                    if (a.ItemSample != null)
                    {
                        ltItemSample.Text = a.ItemSample;
                        ltItemSample.Visible = true;
                        lblItemSample.Visible = true;
                    }
                    if (a.ItemGeneralNote != null)
                    {
                        ltItemGeneralNote.Text = a.ItemGeneralNote;
                        ltItemGeneralNote.Visible = true;
                        lblItemGeneralNote.Visible = true;
                    }
                    if (a.ItemOriginatorIdentifier != null)
                    {
                        ltItemOrigId.Text = a.ItemOriginatorIdentifier;
                        ltItemOrigId.Visible = true;
                        lblItemOrigId.Visible = true;
                    }
                }
            }
        }
        else
        {
            lblError.Text = "Unable to locate selected item.";
            lblError.Visible = true;
            lblItem.Visible = false;
            btnDownLoad.Visible = false;
        }
    }
}